﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Xml;

namespace CNVP.Framework.Utils
{
    public class RSAUtils
    {
        /// <summary>
        /// 创建私钥
        /// </summary>
        /// <returns></returns>
        public RSACryptoServiceProvider CreateRSAProvider()
        {
            RSAParameters parameters1;
            parameters1 = new RSAParameters();
            parameters1.Modulus = RSAClass.Modulus;
            parameters1.Exponent = RSAClass.Exponent;
            parameters1.D = RSAClass.D;
            parameters1.DP = RSAClass.DP;
            parameters1.DQ = RSAClass.DQ;
            parameters1.P = RSAClass.P;
            parameters1.Q = RSAClass.Q;
            parameters1.InverseQ = RSAClass.InverseQ;
            CspParameters parameters2 = new CspParameters();
            parameters2.Flags = CspProviderFlags.UseMachineKeyStore;
            RSACryptoServiceProvider provider1 = new RSACryptoServiceProvider(parameters2);
            provider1.ImportParameters(parameters1);
            return provider1;
        }
        /// <summary>
        /// 创建公钥
        /// </summary>
        /// <returns></returns>
        public RSACryptoServiceProvider CreateRSAEncryptProvider()
        {
            RSAParameters parameters1;
            parameters1 = new RSAParameters();
            parameters1.Modulus = RSAClass.Modulus;
            parameters1.Exponent = RSAClass.Exponent;
            CspParameters parameters2 = new CspParameters();
            parameters2.Flags = CspProviderFlags.UseMachineKeyStore;
            RSACryptoServiceProvider provider1 = new RSACryptoServiceProvider(parameters2);
            provider1.ImportParameters(parameters1);
            return provider1;
        }
        /// <summary>
        /// 创建公钥(格式化)
        /// </summary>
        /// <returns></returns>
        public RSAPKCS1SignatureDeformatter CreateRSADeformatter()
        {
            RSAParameters parameters1;
            parameters1 = new RSAParameters();
            parameters1.Modulus = RSAClass.Modulus;
            parameters1.Exponent = RSAClass.Exponent;
            CspParameters parameters2 = new CspParameters();
            parameters2.Flags = CspProviderFlags.UseMachineKeyStore;
            RSACryptoServiceProvider provider1 = new RSACryptoServiceProvider(parameters2);
            provider1.ImportParameters(parameters1);
            RSAPKCS1SignatureDeformatter deformatter = new RSAPKCS1SignatureDeformatter(provider1);
            deformatter.SetHashAlgorithm("SHA1");
            return deformatter;
        }
        /// <summary>
        /// 创建私钥(格式化)
        /// </summary>
        /// <returns></returns>
        public RSAPKCS1SignatureFormatter CreateRSAFormatter()
        {
            RSAParameters parameters1;
            parameters1 = new RSAParameters();
            parameters1.Modulus = RSAClass.Modulus;
            parameters1.Exponent = RSAClass.Exponent;
            parameters1.D = RSAClass.D;
            parameters1.DP = RSAClass.DP;
            parameters1.DQ = RSAClass.DQ;
            parameters1.P = RSAClass.P;
            parameters1.Q = RSAClass.Q;
            parameters1.InverseQ = RSAClass.InverseQ;
            CspParameters parameters2 = new CspParameters();
            parameters2.Flags = CspProviderFlags.UseMachineKeyStore;
            RSACryptoServiceProvider provider1 = new RSACryptoServiceProvider(parameters2);
            provider1.ImportParameters(parameters1);
            RSAPKCS1SignatureFormatter formatter = new RSAPKCS1SignatureFormatter(provider1);
            formatter.SetHashAlgorithm("SHA1");
            return formatter;
        }
        /// <summary>
        /// 读取数据
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public byte[] GetHashData(byte[] data)
        {
            HashAlgorithm algorithm1 = HashAlgorithm.Create("SHA1");
            return algorithm1.ComputeHash(data);
        }
        /// <summary>
        /// 读取数据
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public byte[] GetHashData(Stream data)
        {
            HashAlgorithm algorithm1 = HashAlgorithm.Create("SHA1");
            return algorithm1.ComputeHash(data);
        }
        /// <summary>
        /// 读取数据
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        private byte[] ReadChild(XmlElement parent, string name)
        {
            XmlElement element1 = (XmlElement)parent.SelectSingleNode(name);
            return Convert.FromBase64String(element1.InnerText);
        }
    }

    class RSAClass
    {
        private static string modulus = @"xl0L0U8rsF40hsGA6f5dn2gBYUG2DT2uH7y0bNC0T2mpvWQyMNBhDdgBdqwJMkdfWKq1hkGFF05oias+uUiHRp2O5YF3S18TVuR7clE9JBV7IGMVUUbh5H2vmKcjR7pgpuui7j41JNHQDk7Df0KqSz1qmtexeHsbfchIeDveSwM=";
        private static string exponent = "AQAB";
        private static string d = @"hr0FHfPNFUuoGjCuREMHjRO0fD+KTYwsDlq7o35QO/Dl30OD5CxnAGRM+jL5PDHGzg+Gpd8zbBVSZNDxFiHx4A1c8/FXRTpdaIDKnFhVFwKOVZtBJIm5dKV1U1oHp9DnjpDksALsIQji5WKJlMyG8GVvFvjZttP5E5VIFMVdSkE=";
        private static string dP = @"NBW8ExpSqlU6icu+CPMiQtAPaz9v6yFZb1+z/pjWV5J3Ve22Iqbv2VABlTIU2afXbX91gOq2q+UNPxXwUQdE4Q==";
        private static string p = @"/I05N1oJ7++6R8o0qvD78hqPGfb38Sy117dVDHDT1qYzhpLAmBMjsEuqVfvLy4KO/QYxjjWhucNpdqDBegt56Q==";
        private static string q = @"yRJpYjnVYTrrl3Poi3HcyFLhPBsnGlNcJ4i9tS3gQNkSHNaS3T9DshdX6PXBvPiBRRCWDRjWAmuE+VNfi2jeCw==";
        private static string dQ = @"kbiON2nCgty4sOKHvcefQm8OXQkwpZuURfKLyiqdQ2IOBmhbt4yJ+x9/zo2NAu72gJ8soAFH2CkkDa2mqNovUQ==";
        private static string inverseQ = @"ZZA2WdfIu8srx9NYvULLnnLDi/GqXTJ1xSZ6dZCQQYSc0XdnLiK7ME9xvFxZgc9PgOtrRNVLlyaPK5BQU7Vi7A==";

        public static byte[] Modulus
        {
            get
            {
                return Convert.FromBase64String(modulus);
            }
        }
        public static byte[] Exponent
        {
            get
            {
                return Convert.FromBase64String(exponent);
            }
        }
        public static byte[] D
        {
            get
            {
                return Convert.FromBase64String(d);
            }
        }
        public static byte[] DP
        {
            get
            {
                return Convert.FromBase64String(dP);
            }
        }
        public static byte[] P
        {
            get
            {
                return Convert.FromBase64String(p);
            }
        }
        public static byte[] Q
        {
            get
            {
                return Convert.FromBase64String(q);
            }
        }
        public static byte[] DQ
        {
            get
            {
                return Convert.FromBase64String(dQ);
            }
        }
        public static byte[] InverseQ
        {
            get
            {
                return Convert.FromBase64String(inverseQ);
            }
        }
    }
}